package com.ssy.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssy.bus.mapper.SupplierMapper;
import com.ssy.bus.pojo.Supplier;
import com.ssy.bus.service.ISupplierService;
import com.ssy.bus.vo.SupplierVo;
import com.ssy.common.CodeMsg;
import com.ssy.common.Constant;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ssy
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements ISupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public PageResult loadAllSupplierPage(SupplierVo supplierVo) {
        Page<Supplier> page = new Page<>(supplierVo.getPage(), supplierVo.getLimit());
        IPage<Supplier> supplierIPage = this.supplierMapper.selectPage(page, new LambdaQueryWrapper<Supplier>()
                .like(StringUtils.hasText(supplierVo.getName()), Supplier::getName, supplierVo.getName())
                .like(StringUtils.hasText(supplierVo.getContacts()), Supplier::getContacts, supplierVo.getContacts())
                .like(StringUtils.hasText(supplierVo.getPhone()), Supplier::getPhone, supplierVo.getPhone())
                .eq(supplierVo.getState() != null, Supplier::getState, supplierVo.getState())
                .orderByDesc(Supplier::getCreateTime));
        return new PageResult(supplierIPage.getTotal(), supplierIPage.getRecords());
    }

    @Override
    public Result insertSupplier(Supplier supplier) {
        if (supplier == null) {
            return new Result(CodeMsg.ADD_ERROR);
        }
        int i = this.supplierMapper.insert(supplier);
        return i > 0 ? new Result(CodeMsg.ADD_SUCCESS) : new Result(CodeMsg.ADD_ERROR);
    }

    @Override
    public Result updateSupplier(Supplier supplier) {
        if (supplier == null) {
            return new Result(CodeMsg.UPDATE_ERROR);
        }
        int i = this.supplierMapper.updateById(supplier);
        return i > 0 ? new Result(CodeMsg.UPDATE_SUCCESS) : new Result(CodeMsg.UPDATE_ERROR);
    }

    @Override
    public Result deleteSupplierById(Integer id) {
        if (id == null) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        int i = this.supplierMapper.deleteById(id);
        return i > 0 ? new Result(CodeMsg.DELETE_SUCCESS) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result bathDeleteSupplier(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        ArrayList<Serializable> idList = new ArrayList<>();
        for (Integer id : ids) {
            idList.add(id);
        }
        this.supplierMapper.deleteBatchIds(idList);
        return new Result(CodeMsg.DELETE_SUCCESS);
    }

    @Override
    public Supplier selectSupplierById(Integer id) {
        return this.supplierMapper.selectById(id);
    }

    @Override
    public Result selectAllSupplier() {
        List<Supplier> suppliers = this.supplierMapper.selectList(new LambdaQueryWrapper<Supplier>()
                .select(Supplier::getId, Supplier::getName)
                .eq(Supplier::getState, Constant.STATE));
        return new Result(suppliers);
    }
}
